package com.hotelsystem.reservation.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.regex.Pattern;

import javax.jws.Oneway;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.synth.SynthSeparatorUI;

import com.hotelsystem.reservations.hotels.Hotel;
import com.hotelsystem.reservations.hotels.HotelInformation;
import com.hotelsystem.reservations.layouts.CreateRoomView;
import com.hotelsystem.reservations.main.MainMenuController;
import com.hotelsystem.reservations.rooms.Room;
import com.hotelsystem.reservations.rooms.RoomManagement;
import com.hotelsystem.reservations.rooms.RoomTypesEnum;

public class CreateRoomController {

	private CreateRoomView createRoomView;
	private RoomManagement roomManagement;
	private MainMenuController mainController;
	private int numberOfRoom;
	private RoomTypesEnum roomTypesEnum;
	private boolean isFreeSmoke;
	private String imagePath;

	public CreateRoomController(MainMenuController mainMenuController) {

		createRoomView = new CreateRoomView();
		roomManagement = new RoomManagement();
		mainController = mainMenuController;
		setListenerForChoose();
		setListenerForSave();
	}

	public void setListenerForChoose() {
		createRoomView.addListenerForChoose(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();

				FileNameExtensionFilter filter = new FileNameExtensionFilter(
						"JPG & GIF Images", "jpg", "gif", "png");
				fileChooser.setFileFilter(filter);
				int returnVal = fileChooser.showOpenDialog(createRoomView
						.getMainPanel());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					imagePath = fileChooser.getSelectedFile().getPath();
				}
			}
		});
	}

	public void setListenerForSave() {
		createRoomView.addListenerForSave(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Room room = new Room();
				boolean isValidNumber = isValidateRoomNumber();
				boolean isCorrect = false;

				if (isValidNumber) {
					int number = Integer.parseInt(createRoomView
							.getRoomNumberText().getText());
					room.setRoomNumber(number);
					isCorrect = true;
				} else {
					JOptionPane.showMessageDialog(
							createRoomView.getMainPanel(), "Invalid number!");
				}
				if (isCorrect) {
					String roomType = createRoomView.getRoomTypeBox()
							.getSelectedItem().toString();

					switch (roomType) {
					case "Room for one person":
						room.setRoomType(RoomTypesEnum.ONE_PERSON_ROOM);
						break;
					case "Room for two persons":
						room.setRoomType(RoomTypesEnum.TWO_PERSONS_ROOM);
						break;
					case "Suite":
						room.setRoomType(RoomTypesEnum.SUITE);
						break;
					case "Luxe Suite":
						room.setRoomType(RoomTypesEnum.LUXE_SUITE);
						break;
					case "Presidential Suite":
						room.setRoomType(RoomTypesEnum.PRESIDENTIAL_SUITE);
						break;
					}

					if (createRoomView.getSmokers().isSelected()) {
						room.setSmokersFree(true);
					} else {
						room.setSmokersFree(false);
					}
					room.setImageFilePath(imagePath);
					Hotel hotel = new Hotel();
					// TODO Create real information for hotels
					HotelInformation hotelInformation = new HotelInformation(
							"Hotel", "", "*", "", true, true, true, true, true,
							true, true, true, true);
					hotel.setHotelInformation(hotelInformation);
					if (roomManagement.addRoom(room, hotel)) {
						JOptionPane.showMessageDialog(
								createRoomView.getMainPanel(), "Room is saved!");
						mainController.goToHome();
					} else {
						JOptionPane.showMessageDialog(
								createRoomView.getMainPanel(), "Room not save!");
					}
				}
			}
		});
	}

	//
	public boolean isValidateRoomNumber() {
		String result = createRoomView.getRoomNumberText().getText();
		if (result.matches(".*\\d.*")) {
			return true;
		} else {
			return false;
		}
	}

	public void createRoom() {

	}

	public JPanel getMainPanel() {
		return createRoomView.getMainPanel();
	}

	public static void main(String[] args) {
		// CreateRoomController controller = new CreateRoomController();
		// JFrame frame = new JFrame();
		// JPanel panel = controller.getMainPanel();
		// frame.add(panel);
		// frame.setSize(900, 900);
		// frame.setVisible(true);
	}
}
